import { message, Modal } from 'antd';
import { FileTextOutlined } from '@ant-design/icons';
import React from 'react';
import qs from 'qs';
import localStorage from '../utils/localStorage';
import PreView from './preView';
import { getConfigure } from '../utils/configure'; // 获取默认配置

export const createUploadUrl = (params = {}) => {
  return `${getConfigure().uploadApi}${qs.stringify(
    {
      ...params,
      Client: getConfigure().client,
      Authorization: localStorage.getItem('token'),
    },
    {
      addQueryPrefix: true,
      allowDots: true, // 多层级对象
    },
  )}`;
};
// 预览文件功能
export const preViewFile = (e) => {
  const fileData = {
    fileId: e.fileId,
    fileCode: e.fileCode,
    fileName: e.fileName,
    Client: getConfigure().client,
    Authorization: localStorage.getItem('token'),
  };
  if (!fileData.fileName && e.name) {
    fileData.fileName = e.name;
  }
  if (!fileData.fileId && e.response && e.response.models) {
    fileData.fileId = e.response.models[0] && e.response.models[0].id;
  }
  if (!fileData.fileCode && e.response && e.response.models) {
    fileData.fileId = e.response.models[0] && e.response.models[0].code;
  }

  const downLoadURL = `${getConfigure().downloadApi}${qs.stringify(
    {
      fileId: fileData.fileId,
      fileCode: fileData.fileCode,
      Client: fileData.Client,
      Authorization: fileData.Authorization,
    },
    { addQueryPrefix: true, allowDots: true },
  )}`;

  Modal.info({
    icon: <FileTextOutlined />,
    title: fileData.fileName,
    content: (
      <p style={{ paddingTop: '10px' }}>
        <PreView
          viewUrl={downLoadURL}
          viewId={fileData.fileId}
          viewCode={fileData.fileCode}
          viewName={fileData.fileName}
          style={{ marginRight: '10px' }}
        />

        <span>
          <a
            href={downLoadURL}
            onClick={() => {
              Modal.destroyAll();
            }}
            download={fileData.fileName}
            target="_blank"
            rel="noopener noreferrer"
          >
            下载
          </a>
        </span>
      </p>
    ),
    okText: '关闭',
    onOk: () => {
      Modal.destroyAll();
    },
  });

  return true;
};
/*
 * 上传文件之前的钩子，参数为上传的文件，若返回 false 则停止上传
 * 上传前过滤一些危险文件
 * */
export const onBeforeUpload = (file, fileList, mess = 0) => {
  const arr = ['cmd', 'exe', 'bat', 'vbs', 'dll'];
  const as = file.name && file.name.split('.')[1];
  if (arr.indexOf(as) >= 0) {
    if (!mess) {
      message.error('该文件存在风险，不可上传!');
    }
    return false;
  }
  const isLt20M = file.size / 1024 / 1024 <= 50;
  if (file.size && !isLt20M) {
    if (!mess) {
      message.error('请选择小于50MB的文件!');
    }
    return false;
  }
  return true;
};

// 生成带参数链接
export const createParamsUrl = (params, target, url) => {
  let baseApi = '';
  if (target === 'download') {
    // 下载
    baseApi = getConfigure().downloadApi;
  } else if (target === 'upload') {
    // 上传
    baseApi = getConfigure().uploadApi;
  } else {
    // 自定义
    baseApi = url;
  }
  return `${baseApi}${qs.stringify(
    {
      ...params,
      Client: getConfigure().client,
      Authorization: localStorage.getItem('token'),
    },
    {
      addQueryPrefix: true,
      allowDots: true, // 多层级对象
    },
  )}`;
};
